// pages/tool/docs/detail/detail.js
const app = getApp();
Page({
    /**
     * 组件的初始数据
     */
    data: {
        id: '',
        type: '',
        info: '',
        extension: '',
        myinfo: '',
        mesg: false,
        pc:false
    },
    /**
     * 页面加载获取id
     */
    onLoad: function (options) {
        app.func.onPageLoad(this, options);
        this.myinfos();

        this.setData({
            id: options.id,
            type: options.type,
            pc: wx.saveFileToDisk != null
        })
    },
    /**
     * 页面渲染
     */
    onShow: function () {
        var id = this.data.id;
        app.func.getPromise('/document/detail/' + this.data.id)
            .then(([code, res]) => {
                this.setData({extension: res.data.extension, info: res.data,});
            })

    },
    myinfos() {
        app.func.getPromise('/my/info')
            .then(([code, res]) => {
                this.setData({
                    myinfo: res.message
                }, () => {

                });
            })
    },
    openy() {
        wx.navigateTo({url: "../preview/preview?id=" + this.data.id})
    },

    download() {
        wx.showLoading();
        wx.downloadFile({
            url: this.data.info.url,
            success: function (res) {
                var filePath = res.tempFilePath
                //仅在pc端使用
                // wx.saveFileToDisk({
                //     filePath: filePath,
                //     complete: function (res) {
                //         wx.removeSavedFile({
                //             filePath: filePath,
                //             complete(res) {
                //                 app.func.toastPromise('已成功下载', 'none', 500);
                //                 wx.hideLoading();
                //             }
                //         })
                //     }
                // })
                wx.saveFile({
                  tempFilePath: filePath,
                  success(res){
                    app.func.toastPromise('已成功下载', 'none', 500);
                    wx.hideLoading();
                  },
                  fail(res){
                    app.func.toastPromise('下载失败', 'none', 500);
                    wx.hideLoading();
                  }
                })
            },
            fail(res) {
                wx.hideLoading();
            }
        });
    },

    // 添加收藏

    detailer(e) {
        // let id = e.currentTarget.dataset.id;
        let index = e.currentTarget.dataset.index;
        app.func.getPromise('/document/favourite/' + this.data.id)
            .then(([code, res]) => {
                if (code == 200) {
                    app.func.toastPromise('已成功收藏', 'none', 500);
                    this.triggerEvent("collectChange", {index: index, value: 1})
                } else {
                    app.func.toastPromise(res.message);
                }
            })
        this.setData({
            ['info.has']: 1
        })

    },
    detailers(e) {

        let index = e.currentTarget.dataset.index;
        app.func.getPromise('/document/cancelFavourite/' + this.data.id)
            .then(([code, res]) => {
                if (code == 200) {

                    app.func.toastPromise('已取消收藏', 'none', 500);
                    this.triggerEvent("collectChange", {index: index, value: 0})
                } else {
                    app.func.toastPromise(res.message);
                }
            })
        this.setData({
            ['info.has']: 0
        })
    },
    /**
     * 组件的方法列表
     */
    methods: {}
})
